<style>
	
</style>
<template>
    <div>
        <header class="admin-container-header clearfix">
            <div class="header-group">
                <h4 class="title">内容</h4> <h5 class="sub-title">模型</h5>
            </div>
            <div class="float-right">
                <button type="button" class="float-right ivu-btn ivu-btn-primary ivu-btn-circle" @click='addItemStatus'>
                    <span><Icon type="plus-round"></Icon> 添加模型</span>
                </button>
            </div>
        </header>
        <main class="admin-container-body" style="height: calc(100% - 50px)">
            <section class="panel-box">
            <section class="panel-box-body">
                
                <section class="diy-table-list" v-cloak>
                    <div class="diy-table-item diy-table-item-header">
                        <div class="list-unstyled row">
                            <div class="col-md-2">标题</div>
                            <div class="col-md-2">名称</div>
                            <div class="col-md-2">操作</div>
                        </div>
                    </div>
                    <div v-if='itemList.length' class="diy-table-body">
                        <div class="diy-table-item" v-for='(item,index) in itemList' >
                            <div class="list-unstyled row">
                                <div class="col-md-2 text-999"><span>{{item.title}}</span></div>
                                <div class="col-md-2 text-999"><span>{{item.name}}</span></div>
                                <div class="col-md-2">
                                    <i-button type="button" class="ivu-btn ivu-btn-error ivu-btn-small" :disabled='item.id == 1 || item.id == 2 || item.id == 3 || item.id == 4'  @click='itemDel(item)'>删除</i-button>
                                    <i-button type="button" class="ivu-btn ivu-btn-small" @click='dialogItemEdit(item)'>修改</i-button>
                                    <i-button type="button" class="ivu-btn ivu-btn-primary ivu-btn-small" @click='fieldSetting(item)'>字段管理</i-button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="no-block" v-else style="height: calc(100% - 50px)">
                        <Icon type="ios-filing-outline"></Icon>
                        <p>暂无数据</p>
                    </div>
                </section>
                   
            </section>
        </section>
    </main>
    <Modal :title="dialogTitle" v-model="dialog" size="small" v-cloak>
        <i-form :model="item" :rules="item.rules" ref="item" :label-width="80">
            <Form-Item label="标题" prop="title">
                <i-input v-model="item.title" placeholder="例：文章"></i-input>
            </Form-Item>
            <Form-Item label="名称" prop="name">
                <i-input v-model="item.name" :disabled='item.id != ""' placeholder="例：article"></i-input>
            </Form-Item>
        </i-form>
        <div slot="footer" class="dialog-footer" v-cloak>
            <i-button @click="dialog = false">取 消</i-button>
            <i-button type="primary" @click="itemSave()">确 定</i-button>
        </div>
    </Modal>
    </div>
</template>

<script>
export default {
    data () {
        return {
            itemList: [],
            dialog: false,
            dialogTitle: '添加模型',
            item:{
                id: '',
                title: '',
                name: '',
            }
        }
    },
    mounted: function() {
        this.getItemList();
    },
    methods: {
        fieldSetting(item) {
            this.$router.push({
                name: 'articleField', params: { mod:'article','id': item.name}
            });
        },
        getItemList: function() {
            this.$ssy.ajax('{$domain}/article/ApiAdminArticleModel/itemList', {
            }).then(res => {
                if (res.code == 1) {
                    this.itemList = res.data;
                }
            });
        },
        itemDel: function(item) {
            this.$Modal.confirm({
                title: '消息提示',
                content: '<p>确定删除么？删除后不可恢复</p>',
                onOk: () => {
                    this.$ssy.ajax('{$domain}/article/ApiAdminArticleModel/itemDel', {
                        id: item.id,
                    }).then(res => {
                            if (res.code == 1) {
                            this.$Message.success('删除成功');
                            this.getItemList();
                        }
                    });
                },
                onCancel: () => {
                }
            });
        },
        addItemStatus: function() {
            this.dialogTitle = '添加模型';
            this.dialog = true;
            this.item.id = '';
            this.item.title = '';
            this.item.name = '';
        },
        dialogItemEdit: function(item) {
            this.dialogTitle = '修改模型';
            this.dialog = true;
            this.item.id = item.id;
            this.item.title = item.title;
            this.item.name = item.name;
        },
        
        itemSave: function() {
            if (!this.item.title) {
                this.$Message.error('标题不能为空');
                return false;
            }
            if (!this.item.name) {
                this.$Message.error('名称不能为空');
                return false;
            }
            var parent = /^[a-zA-Z0-9_-]+$/;
            if (!parent.test(this.item.name)) {
                this.$Message.error('名称输入值不支持');
                return false;
            }
            if (this.item.id) {
                this.$ssy.ajax('{$domain}/article/ApiAdminArticleModel/itemEdit', {
                    id: this.item.id,
                    title: this.item.title,
                }).then(res => {
                    if (res.code == 1) {
                        this.$Message.success(res.msg);
                        this.getItemList();
                        this.dialog = false;
                    }
                });
            } else {
                this.$ssy.ajax('{$domain}/article/ApiAdminArticleModel/itemAdd', {
                    title: this.item.title,
                    name:  this.item.name,
                }).then(res => {
                    if (res.code == 1) {
                        this.$Message.success(res.msg);
                        this.getItemList();
                        this.dialog = false;
                    }
                });
            }
        },
        
    }
}
</script>
